import Mock from 'mockjs';
import { {{ table.derived.naming.class_name }} } from './{{ table.derived.naming.class_name }}';

const mockTemplate = {
{% for column in columns.values() %}  {{ column.naming.field_name }}: '{{ column.derived.ts.mock_rule }}',
{% endfor %}};

// 模拟数据存储
let mockData = Mock.mock({
  'items|100': [mockTemplate]
}).items;

// 获取{{ table.common.label }}列表
export const getMock{{ table.derived.naming.class_name }}Data = (pageNumber: number, pageSize: number) => {
  const start = (pageNumber - 1) * pageSize;
  const records = mockData.slice(start, start + pageSize);

  return Promise.resolve({
    msg: "SUCCESS",
    code: 0,
    data: {
      current: pageNumber,
      total: mockData.length,
      pages: Math.ceil(mockData.length / pageSize),
      size: pageSize,
      records
    }
  });
};

// 添加{{ table.common.label }}
export const mockAdd{{ table.derived.naming.class_name }} = (entity: Partial<{{ table.derived.naming.class_name }}>) => {
  const now = new Date().toISOString();
  const new{{ table.derived.naming.class_name }} = {
    ...entity,
    id: mockData.length + 1,
    createdAt: now,
    updatedAt: now
  };
  mockData.push(new{{ table.derived.naming.class_name }});
  return Promise.resolve({ code: 0, msg: '{{ table.derived.naming.class_name }} added successfully' });
};

// 编辑{{ table.common.label }}
export const mockEdit{{ table.derived.naming.class_name }} = (id: any, valueEntity: Partial<{{ table.derived.naming.class_name }}>) => {
  const now = new Date().toISOString();
  mockData = mockData.map(item =>
    item.id === id ? { ...item, ...valueEntity, updatedAt: now } : item
  );
  return Promise.resolve({ code: 0, msg: '{{ table.derived.naming.class_name }} updated successfully' });
};

// 删除{{ table.common.label }}
export const mockDelete{{ table.derived.naming.class_name }} = (id: any) => {
  mockData = mockData.filter(item => item.id !== id);
  return Promise.resolve({ code: 0, msg: '{{ table.derived.naming.class_name }} deleted successfully' });
};

// 获取{{ table.common.label }}详情
export const mockFetch{{ table.derived.naming.class_name }}Detail = (id: any) => {
  const record = mockData.find(item => item.id === id);
  if (record) {
    return Promise.resolve({ code: 0, data: record });
  }
  return Promise.reject(new Error('{{ table.derived.naming.class_name }} not found'));
};